package com.itboye.ihomebank.activity.guanjia;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.MyGuanJiaAdapter;
import com.itboye.ihomebank.base.BaseFragment;
import com.itboye.ihomebank.bean.ChanKanWeiXiuBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.custom.XListView;
import com.itboye.ihomebank.presenter.RepairPersenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 我的生活管家（派单中,维修中，待评价）
 */
@SuppressLint("ValidFragment")
public class ActivityMyGuanJia extends BaseFragment implements Observer {
	SmartRefreshLayout refresh;
	ListView guanli_xlistView;
	TextView imgEmpty;
	RepairPersenter repairPersenter;

	private int type;
	List<ChanKanWeiXiuBean> list;
	MyGuanJiaAdapter adapter;
	String uid;
	private int pageNo = 1;
	private MaterialHeader mMaterialHeader;
	public ActivityMyGuanJia(int type) {
		this.type = type;
	}

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_my_guanjia;
	}
	@Override
	public void onMyActivityCreated() {
		repairPersenter = new RepairPersenter(this);
		list=new ArrayList<>();
		showProgressDialog("数据加载中,请稍后", true);
		uid= (String) SPUtils.get(getActivity(), null,SPContants.USER_ID, "");
		caozuo();
		adapter = new MyGuanJiaAdapter(type,getActivity(), list,R.layout.item_my_guanjia);
		guanli_xlistView.setAdapter(adapter);
	}
	private void caozuo(){
		mMaterialHeader = (MaterialHeader)refresh.getRefreshHeader();
		refresh.setPrimaryColorsId(android.R.color.holo_green_light, android.R.color.white);
		refresh.setEnableAutoLoadmore(true);//开启自动加载功能（非必须）

		//触发自动刷新
		refresh.autoRefresh();
		refresh.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh(RefreshLayout refreshlayout) {
				refreshlayout.getLayout().postDelayed(new Runnable() {
					@Override
					public void run() {
						pageNo = 1;
						repairPersenter.chanKanWeiXiu(uid, type+"");
					}
				}, 0);
			}
		});

		//进行加载更多的逻辑处理
		refresh.setOnLoadmoreListener(new OnLoadmoreListener() {
			@Override
			public void onLoadmore(RefreshLayout refreshlayout) {
				refreshlayout.getLayout().postDelayed(new Runnable() {
					@Override
					public void run() {
						pageNo = 1;
						repairPersenter.chanKanWeiXiu(uid, type+"");
					}
				}, 0);
			}
		});
		//refresh.setRefreshHeader(new MaterialHeader(getActivity()).setShowBezierWave(true));
		refresh.setRefreshFooter(new BallPulseFooter(getActivity()).setSpinnerStyle(SpinnerStyle.Scale));
		refresh.finishLoadmore();
	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (!entity.getCode().equals(0)) {
				if (entity.getEventType() == RepairPersenter.weixiuZhuangTai_success) {
					List<ChanKanWeiXiuBean> bean = (List<ChanKanWeiXiuBean>) entity
							.getData();
					if (pageNo == 1) {//下拉刷新
						list.clear();
						adapter.notifyDataSetChanged();
						//结束加载
						refresh.finishRefresh();
						refresh.finishLoadmore();
					}
					if (bean != null) {
						if (bean.size() <= 0) {
							imgEmpty.setVisibility(View.VISIBLE);
						} else {
							imgEmpty.setVisibility(View.GONE);
							list.addAll(bean);
						}
					}
				} else if (entity.getEventType() == RepairPersenter.weixiuZhuangTai_fail) {
					ByAlert.alert(entity.getMsg());
					refresh.finishLoadmore();
					refresh.finishRefresh();
				}
			}
		}
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
